package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.test.ActivityControler;
import com.example.test.AddCardActivity;
import com.example.test.R;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class AddCardFragment extends BaseFragment implements OnClickListener {

	@ViewInject(R.id.spinner_bank)
	private Spinner sp_bank;
	@ViewInject(R.id.btn_addcard)
	private Button btn_addcard;
	@ViewInject(R.id.et_cardnumber)
	private EditText et_cardnumber;
	private List<String> list = new ArrayList<String>();
	private ArrayAdapter<String> adapter;
	private String cardnumber;
	private long res;

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		btn_addcard.setOnClickListener(this);
		list.add("工商银行");
		list.add("农村商业银行");
		list.add("农业银行");
		list.add("邮政储蓄");
		list.add("交通银行");
		list.add("招商银行");
		adapter = new ArrayAdapter<String>(ct,
				android.R.layout.simple_spinner_item, list);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_bank.setAdapter(adapter);
		sp_bank.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				view.setVisibility(View.VISIBLE); 
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				parent.setVisibility(View.VISIBLE); 
			}
		});
	}

	@Override
	public View initView(LayoutInflater inflater) {

		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.addcard_layout, null);
		ViewUtils.inject(this,view);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_addcard:
          if(checkForm()){
        	  DbDaoUtils daoUtils=new DbDaoUtils(ct);
        	  String bankname=sp_bank.getSelectedItem().toString();
        	  res = daoUtils.insertCard(bankname, cardnumber, 5000);
        	  if(res>0){
        		  Toast.makeText(ct, "添加成功", 0).show();
        		  ((AddCardActivity)ct).finish();
        		  ActivityControler.finishAll();
        	  }else {
        		  Toast.makeText(ct, "添加失败", 0).show();
			}
        	  
          }
			break;

		default:
			break;
		}
	}

	private boolean checkForm() {
		// TODO Auto-generated method stub
		cardnumber = et_cardnumber.getEditableText().toString().trim();		
       if(TextUtils.isEmpty(cardnumber)){
    	   Toast.makeText(ct, "卡号不能为空", Toast.LENGTH_SHORT).show();
    	   return false;
       }		
		return true;
	}
}
